﻿using Dapper;
using Furion.DataValidation;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Gear.Application.ErrorControl;
using Gear.Application.Service.ADShows.Dtos;
using Gear.Application.Verification.Condition;
using Gear.Core;
using Gear.NoSql.Core.Redis;
using Gear.Web.Api.DynamicRouter;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Gear.Application.Service.BaseService
{
    /// <summary>
    /// 测试模块
    /// </summary>
    [ApiDescriptionSettings("TempTest")]

    public class GearDapperTest : IDynamicApiController
    {
        private readonly IDapperRepository _dapperRepository;
        private readonly ISqlSugarRepository _repository; // 仓储对象：封装简单的CRUD
        private readonly SqlSugarClient _db; // 核心对象：拥有完整的SqlSugar全部功能
        private readonly IAdo _dbAdo;// Ado重载对象 支持原生sql写法

        public GearDapperTest(IDapperRepository dapperRepository, ISqlSugarRepository sqlSugarRepository)
        {
            _dapperRepository = dapperRepository;
            _repository = sqlSugarRepository;
            _db = _repository.Context;
            _dbAdo = _db.Ado;
        }

        public dynamic GetAdshow(string remark)
        {
            try
            {
                var data1 = _dapperRepository.Query<dynamic>("select * from adshow where remark=@remark", new { remark }).ToList();
                return data1;
            }
            catch(Exception)
            {
                throw;
            }
            
        }
        public List<dynamic> GetAdshow1(int id)
        {
            try
            {
                var data1 = _db.Queryable<dynamic>().AS("adshow").Where("id=@id", new { id }).ToList();
                return data1;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<dynamic> GetAdshowAdo(string remark)
        {
            try
            {
                var data1 = _dbAdo.SqlQuery<dynamic>("select * from adshow where remark=@remark", new { remark }).ToList();
                return data1;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<Adshow> GetAdshowrSugar(string remark)
        {
            try
            {
                var data1 = _db.Queryable<Adshow>().Where(it => it.Remark == remark).ToList();
                return data1;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<dynamic> GetAdshow2(int id)
        {
            try
            {
                var data1 = _dbAdo.SqlQuery<dynamic>("select * from adshow where id=@id", new { id }).ToList();
                return data1;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public resDic GetSysDic(int parentid)
        {
            try
            {
                var resDic = new resDic();
                var data1 = _dapperRepository.Query<object>("SELECT codekey label,codeValue_int value FROM dictionary_sys where parentid=@parentid limit 0,10 ", new { parentid}).ToList();
                var total = _dapperRepository.Query<int>("SELECT count(1) FROM dictionary_sys where parentid=@parentid", new { parentid }).FirstOrDefault();
                resDic.data = data1;
                resDic.total = total;
                return resDic;
            }
            catch (Exception)
            {
                throw;
            }

        }

        public resDic GetSysDic(int parentid, int curPage, int pageSize)
        {
            try
            {
                var resDic = new resDic();
                var pageCnt = (curPage - 1) * pageSize;
                var data1 = _dapperRepository.Query<object>("SELECT codekey label,codeValue_int value FROM dictionary_sys where parentid=@parentid  LIMIT @pageCnt,@pageSize", new { parentid, pageCnt, pageSize }).ToList();
                var total = _dapperRepository.Query<int>("SELECT count(1) FROM dictionary_sys where parentid=@parentid", new { parentid }).FirstOrDefault();
                resDic.data = data1;
                resDic.total = total;
                return resDic;
            }
            catch (Exception)
            {
                throw;
            }

        }
        public resDic GetSysDic(int parentid, int curPage, int pageSize, string codekey = null)
        {
            try
            {
                var resDic = new resDic();
                var pageCnt = (curPage - 1) * pageSize;
                var data1 = _dapperRepository.Query<object>("SELECT codekey label,codeValue_int value FROM dictionary_sys where parentid=@parentid and codekey like concat('%',@codekey,'%') LIMIT @pageCnt,@pageSize", new { parentid, pageCnt, pageSize }).ToList();
                var total = _dapperRepository.Query<int>("SELECT count(1) FROM dictionary_sys where parentid=@parentid", new { parentid }).FirstOrDefault();
                resDic.data = data1;
                resDic.total = total;
                return resDic;
            }
            catch (Exception)
            {
                throw;
            }

         }

        /// <summary>
        /// PostProcedure
        /// </summary>
        /// <returns></returns>
        public object PostProcedure()
        {
            // 定义基础参数
            List<SugarParameter> paramster = new List<SugarParameter>();
            object a = 0;
            object b = "success";
            object c = "";
            var p_in = new SugarParameter("@p_in", a);
            var p_status = new SugarParameter("@p_status", a, true); // 存储过程执行状态
            var p_message = new SugarParameter("@p_message", b, true); // 存储过程执行消息
            var p_result = new SugarParameter("@p_result", c, true); // 存储过程执行返回结果
            paramster.Add(p_in);
            paramster.Add(p_status);
            paramster.Add(p_message);
            paramster.Add(p_result);

            _repository.Context.Ado.UseStoredProcedure().SqlQuery<object>("in1_param", paramster);

            return new { p_status = p_status.Value, p_message = p_message.Value, p_result = p_result.Value };
        }

    }

    public class GearRedisTest
    {
        public IRedisFuncProvider _redisFuncProvider;
        public GearRedisTest(IRedisFuncProvider redisFuncProvider)
        {
            _redisFuncProvider = redisFuncProvider;
        }
        public string Get(int id)
        {
            var result = _redisFuncProvider.GetStringKey<int>("key1");
            //若redis没有数据,则取数据并设置redis
            if (result == default(int))
            {
                Console.WriteLine("redisResult is empty");
                _redisFuncProvider.SetStringKey("key1", id.ToString());
                return id.ToString();
            }
            return result.ToString();//有则直接返回

        }

    }

    public class resDic
    {
        public List<object> data { get; set; }
        public int total { get; set; }
    }


}
